package com.zheng.pvz.view 
{
	import com.utils.DebugTrace;
	import com.zheng.pvz.action.IAction;
	import com.zheng.pvz.data.DataConst;
	import com.zheng.pvz.model.proxy.DisplayProxy;
	import com.zheng.pvz.utils.NumberToCoo;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;

	/**
	 * 父类，被继承
	 * @author Mr.zheng
	 */
	public class HitObjectClass extends BaseObject implements IHitObject
	{
		public var contain:DisplayObjectContainer;
		public var proxy:DisplayProxy;
		public var radius:int;
		public var actionArr:Array
		public var statusArr:Array;
		
		private var _row:int;
		private var _cols:int
		
		public function HitObjectClass(contain:DisplayObjectContainer,proxy:DisplayProxy)
		{
			this.contain=contain;
			this.proxy=proxy;
			this.radius=10;
			this.actionArr=[];
			this.statusArr=[];
			
			this.mouseChildren=false;
			this.mouseEnabled=false;
			
			this.addEvent();
		}
		
		public function get row():int{
			return proxy.row;
		}
		public function get cols():int{
			return proxy.cols
		}
		public function set row(value:int):void{
			proxy.row=value;
		}
		public function set cols(value:int):void{
			proxy.cols=value;
		}
		/**
		 * 添加对象的状态 
		 * @param str	状态的名称
		 * @param mc	名称对应的mc
		 * 
		 */
		public function addStatus(str:String,mc:MovieClip):void{
			if(this.statusArr[str])return;
			if(str!=DataConst.STATUS_NORMAL){
				if(str!=DataConst.STATUS_DEL){
					mc.addFrameScript(mc.totalFrames-1,onLastFrame);
				}else{
					mc.addFrameScript(mc.totalFrames-1,delThis);
				}
			}
			this.statusArr[str]=mc; 
			mc.gotoAndStop(1);
			
		}
		/**
		 *摆放 
		 * 
		 */
		public function put():void{
			changedStatus(this.proxy.status);
			BaseObject(contain).addObject(this);
			NumberToCoo.getInstance().numToCoo(this,new Point(this.row,this.cols));
			//if(DebugTrace.ballCanShow)this.showBall();
		}
		public function addAction(action:IAction):void{
			
		}
		/**
		 *  
		 * @param str
		 * 
		 */
		public function changedStatus(str:String):void{
			clearThis();
			if(!this.statusArr[str])DebugTrace.dtrace("code info HitObjectClass.as:没有"+str+"对应的动画");
			this.addChild(this.statusArr[str]);
			MovieClip(this.statusArr[str]).play();
		}
		public function delThis():void{
			clearThis();
			removeEvent();
			ElementSprite(contain).removeObject(this);
		}
		public function getType():String{
			return proxy.vo.displayName;
		}
		
		//私有方法===============================================================
		private function addEvent():void{
			proxy.addEventListener(DisplayProxy.CHANGED_STATUS,onChangedStatus);
		}
		private function removeEvent():void{
			proxy.removeEventListener(DisplayProxy.CHANGED_STATUS,onChangedStatus);
		}
		private function onChangedStatus(e:Event):void{
			changedStatus(proxy.status);
		}
		private function onLastFrame():void{
			changedStatus(DataConst.STATUS_NORMAL);
		}
		private function clearThis():void{
			if(this.numChildren>0){
				var num:int=this.numChildren;
				for(var i:int=0;i<num;i++){
					var mc:*=this.removeChildAt(0);
					if(mc is MovieClip){
						mc.gotoAndStop(1);
					}
				}
			}
		}
		/*
		public function showBall():void{
			var ball:Sprite=new Sprite();
			ball.graphics.beginFill(0xff00ff,.3);
			ball.graphics.drawCircle(0,0,this.radius);
			ball.graphics.endFill();
			addChild(ball);
		}*/
		
	}

}